package com.example.topicfromjinzhuan2024national

import android.app.Activity
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.RecyclerView

class HomeRecyAdapter(list:ArrayList<HomeRecyListObject>):RecyclerView.Adapter<HomeRecyAdapter.ViewHolder>() {
    var lists=list
    class ViewHolder(view:View):RecyclerView.ViewHolder(view) {
        var img:ImageView=view.findViewById(R.id.home_recy_img)
        var text:TextView=view.findViewById(R.id.home_recy_text)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        var view=LayoutInflater.from(parent.context).inflate(R.layout.home_recy,parent,false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return lists.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        var listObject1= lists[position]
        holder.img.setImageResource(listObject1.img)
        holder.text.setText(listObject1.text)

        holder.itemView.setOnClickListener {
            if(position==7){
                var dialog=AlertDialog.Builder(holder.itemView.context)
                dialog.setTitle("提示")
                dialog.setMessage("是否确认退出？")
                dialog.setNegativeButton("取消"){dialog,whitch->dialog.cancel()}
                dialog.setPositiveButton("确认"){dialog,whitch->(holder.itemView.context as Activity).finishAffinity()}
                //.finish()适用于只需要结束当前Activity的情况，比如用户完成了一个特定的操作后返回到前一个界面。
                //.finishAffinity()适用于需要一次性结束多个相关Activity的情况，比如用户登出应用时，你可能希望结束所有与用户会话相关的Activity，而不仅仅是当前的一个。
                dialog.create()
                dialog.show()
            }
            else if(position==0){holder.itemView.context.startActivity(Intent(holder.itemView.context,TrafficLightManage::class.java))}
            else if(position==1){holder.itemView.context.startActivity(Intent(holder.itemView.context,BreakRules::class.java))}
        }
    }
}